Ext.define('AmpleHarvest.util.utility', 
{
    extend			: 'Ext.util.Observable',
    listenerName	: 'dataResponse',
    bool_destroy	: true,
    listeners		:
	{
		responseFailure	: function(e)
		{
			if(this.bool_destory)
			{
				Ext.destroy(this.AHcaller);
			}
		},
		
		responseSuccess	: function()
		{
			this.AHcaller.fireEvent(this.listenerName, this.obj_return);
		}
	},
	
	constructor	: function(config)
	{
		this.addEvents('responseFailure', 'responseSuccess');
		this.callParent();
	},
	
	getData	: function(str_path, obj_params, obj_jsonData, str_method, obj_passed, obj_headers)
	{
		obj_headers['X-ProxiedEntitiesChain'] = obj_config.user.dn;
		Ext.Ajax.request(
				{
					url		: str_path,
					headers	: obj_headers,
					method	: str_method,
					jsonData: obj_jsonData,
					params	: obj_params,
					timeout	: 100000,
					scope	: this,
					success	: function(response)
					{
						if(response.status == '200')
						{
							this.AHcaller.fireEvent('200', response);
						}
						if(response.status == '404'||response.status == '500')
						{
							this.fireEvent('responseFailure', 'Unable to load resource url');
						}
						else
						{
							if(obj_passed.callback != true)
							{
								var obj_data = Ext.decode(response.responseText);
								obj_data.ajaxProperties = {
										url		: str_path,
										method	: str_method,
										params	: obj_params,
										config	: obj_passed
								};
								this.checkResponse(obj_data);
							} else {
								eval(response.responseText);
							}
							
						}
					},
					failure	: function(e)
					{
						this.fireEvent('responseFailure', e.statusText);
					}
				});
	},
	
	checkResponse	: function(response)
	{
		if(Ext.isObject(response))
		{
			if(this.AHcaller.verifyData)
			{
				if(this.AHcaller.verifyData(response))
				{
					
					this.checkTransform(response);
					this.fireEvent('responseSuccess');
					return;
				}
			} else {
				this.checkTransform(response);
				this.fireEvent('responseSuccess');
				return;
			}
		}
		this.fireEvent('responseFailure', 'no content found');
	},
	
	checkTransform	: function(response)
	{
		if(this.AHcaller.transformData)
		{
			response = this.AHcaller.transformData(response);
		}
		this.obj_return = response;
	}
});